package com.hcinfos.gsoo.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.ImageInfo;
import com.hcinfos.gsoo.interaze.DataChangeCallback;

public class UploadImgAdapter extends BaseAdapter {

	public List<ImageInfo> list;
	private Context context;
	private LayoutInflater mInflater;
	private BitmapFactory.Options options;

	public UploadImgAdapter(Context context, List<ImageInfo> list) {
		options = new BitmapFactory.Options();
		options.inSampleSize = 8;
		this.list = list;
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {

		final ImgHolder holder;
		if (convertView == null) {
			holder = new ImgHolder();
			convertView = mInflater.inflate(R.layout.upload_img_item, null);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.img_iv);
			holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			holder.deleteView = (ImageView) convertView
					.findViewById(R.id.delete);
			convertView.setTag(holder);
		} else {
			holder = (ImgHolder) convertView.getTag();
		}
		ImageInfo info = list.get(position);

		if (position == 0) {
			holder.deleteView.setVisibility(View.GONE);
			holder.imageView.setImageBitmap(info.getBitmap());
		} else {
			holder.imageView.setImageBitmap(BitmapFactory.decodeFile(
					info.getPath(), options));
		}

		holder.deleteView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				list.remove(position);
				if (callBack != null) {
					callBack.dataChange(list);
				}
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

	private final class ImgHolder {
		ImageView imageView;
		ImageView deleteView;
	}

	public void setDataChange(List<ImageInfo> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public int position = -1;

	public void setPosition(int position) {
		this.position = position;
	}

	public DataChangeCallback callBack;

	public void setDataChangeCallback(DataChangeCallback call) {
		this.callBack = call;
	}
}
